﻿Imports System
Imports System.ComponentModel
Imports System.Globalization
Imports System.Workflow.ComponentModel.Compiler

Namespace System.Workflow.ComponentModel.Design
    Friend Class TypePropertyTypeConverter
        Inherits TypeConverter
        ' Methods
        Public Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal sourceType As Type) As Boolean
            If (sourceType Is Nothing) Then
                Throw New ArgumentNullException("sourceType")
            End If
            Return (Object.Equals(sourceType, GetType(String)) OrElse MyBase.CanConvertFrom(context, sourceType))
        End Function

        Public Overrides Function CanConvertTo(ByVal context As ITypeDescriptorContext, ByVal destinationType As Type) As Boolean
            If (destinationType Is Nothing) Then
                Throw New ArgumentNullException("destinationType")
            End If
            Return (Object.Equals(destinationType, GetType(Type)) OrElse MyBase.CanConvertTo(context, destinationType))
        End Function

        Public Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal valueToConvert As Object) As Object
            Dim str As String = [TryCast](valueToConvert,String)
            If String.IsNullOrEmpty(str) Then
                Return Nothing
            End If
            If (Not context Is Nothing) Then
                Dim service As ITypeProvider = [TryCast](context.GetService(GetType(ITypeProvider)),ITypeProvider)
                If (Not service Is Nothing) Then
                    Return service.GetType(str, True)
                End If
            End If
            Return MyBase.ConvertFrom(context, culture, valueToConvert)
        End Function

        Public Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object, ByVal destinationType As Type) As Object
            If (TypeOf value Is Type AndAlso Object.Equals(destinationType, GetType(String))) Then
                Return DirectCast(value, Type).FullName
            End If
            Return MyBase.ConvertTo(context, culture, value, destinationType)
        End Function

    End Class
End Namespace

